<template>
  <div class="register-page">
    <div class="register-box">
      <div class="register-title">邮箱注册</div>
      <div class="input-group">
        <div class="input-item">
          <input 
            class="input" 
            placeholder="请输入邮箱" 
            v-model="account" 
            @input="onEmailInput" 
          />
        </div>
        <div class="input-item">
          <input 
            class="input" 
            placeholder="请输入密码" 
            type="password" 
            v-model="password" 
            @input="onPasswordInput" 
          />
        </div>
        <div class="input-item">
          <input 
            class="input" 
            placeholder="请确认密码" 
            type="password" 
            v-model="confirmPassword" 
            @input="onConfirmPasswordInput" 
          />
        </div>
      </div>
      <div class="tips">请填写有效邮箱和6位以上密码</div>
      <button class="register-btn" @click="onRegister">注册</button>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { showToast, showLoadingToast, closeToast } from 'vant'
import { register } from '@/api/auth'

const router = useRouter()

// 响应式数据
const account = ref('')
const password = ref('')
const confirmPassword = ref('')

// 邮箱输入
const onEmailInput = (e) => {
  account.value = e.target.value
}

// 密码输入
const onPasswordInput = (e) => {
  password.value = e.target.value
}

// 确认密码输入
const onConfirmPasswordInput = (e) => {
  confirmPassword.value = e.target.value
}

// 注册
const onRegister = async () => {
  const accountValue = account.value
  const passwordValue = password.value
  const confirmPasswordValue = confirmPassword.value
  
  if (!accountValue || !passwordValue || !confirmPasswordValue) {
    showToast('请填写完整信息')
    return
  }
  
  if (passwordValue !== confirmPasswordValue) {
    showToast('两次密码不一致')
    return
  }
  
  if (passwordValue.length < 6) {
    showToast('密码长度不能少于6位')
    return
  }
  
  showLoadingToast({
    message: '注册中...',
    forbidClick: true,
  })
  
  try {
    const res = await register({ account: accountValue, password: passwordValue })
    
    closeToast()
    
    if (res.code === 0) {
      showToast({
        type: 'success',
        message: '注册成功'
      })
      
      setTimeout(() => {
        router.push('/login')
      }, 1500)
    } else {
      showToast(res.message || '注册失败')
    }
  } catch (error) {
    closeToast()
    showToast('注册失败')
  }
}
</script>

<style lang="scss" scoped>
.register-page {
  background: #f3f4f6;
  display: flex;
  flex-direction: column;
  align-items: center;
  min-height: 100vh;
  padding: 20px;
  box-sizing: border-box;
}

.register-box {
  width: 100%;
  max-width: 400px;
  background: #fff;
  min-height: 300px;
  border-radius: 12px;
  box-shadow: 0 2px 12px 0 rgba(119, 163, 149, 0.10);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  padding: 30px 20px 20px 20px;
  margin-top: 20px;
}

.register-title {
  font-size: 20px;
  font-weight: bold;
  color: #157658;
  margin-bottom: 30px;
}

.input-group {
  width: 100%;
  margin-bottom: 20px;
}

.input-item {
  display: flex;
  align-items: center;
  width: 100%;
  padding: 0 12px;
  margin: 10px 0;
  height: 44px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.03);
  border: 1px solid #f0f0f0;
  position: relative;
  overflow: hidden;
}

.input {
  flex: 1;
  border: none;
  outline: none;
  font-size: 16px;
  color: #333;
  background: transparent;
  padding: 8px 0;
  
  &::placeholder {
    color: #999;
  }
  
  &:focus {
    border-bottom: 1px solid #007aff;
  }
}

.tips {
  font-size: 12px;
  color: #999;
  margin-bottom: 20px;
  text-align: center;
}

.register-btn {
  width: 90%;
  height: 44px;
  background-color: #157658;
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: 18px;
  font-weight: bold;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 8px 0 rgba(76, 207, 161, 0.10);
  letter-spacing: 2px;
  cursor: pointer;
  
  &:active {
    opacity: 0.85;
  }
}
</style> 